﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace СityDelivery
{
	public partial class SettingParametersForm : Form
	{
		public SettingParametersForm()
		{
			InitializeComponent();

			for (int index = 0; index < Data.Shops.Count; index++)
			{
				int item = Data.Shops[index];

				String text = Data.nodesList[item].StreetArray[0] + " и " + Data.nodesList[item].StreetArray[1];
				timeShops.Rows.Add(text);

			}

		}


		private void buttonOK_Click(object sender, EventArgs e)
		{
			Data.shopsInfoList.Clear();
			bool inputFlag = false;

			foreach (DataGridViewRow row in timeShops.Rows)
				foreach (DataGridViewCell cell in row.Cells)
					if (cell.Value == null || cell.Value.Equals(""))
						inputFlag = true;
			if (!speedBox.Text.Equals("") && !valueBox.Text.Equals("") && !inputFlag)
			{

				Data.speed = Convert.ToInt32(speedBox.Text);
				Data.weightRestriction = Convert.ToSingle(valueBox.Text);

				int i = 0;

				foreach (DataGridViewRow row in timeShops.Rows)
				{
					Data.shopsInfoList.Add(new Data.shopsInfo(Data.Shops[i], Convert.ToSingle(row.Cells[1].Value),
											Convert.ToSingle(row.Cells[2].Value) * 60));
					i++;
				}
				Data.parametersFlag = true;
				this.Close();
			}

			else
			{
				MessageBox.Show("Вы не ввели параметры!", "Ошибка");
			}
		}

		private void speedBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!(Char.IsNumber(e.KeyChar) || e.KeyChar == 8))
				e.Handled = true;
		}

		private void valueBox_KeyPress(object sender, KeyPressEventArgs e)
		{

			if (!(Char.IsDigit(e.KeyChar) || e.KeyChar == 8))
				e.Handled = true;
		}

		private void timeShops_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
		{
			DataGridViewTextBoxEditingControl tb = (DataGridViewTextBoxEditingControl)e.Control;
			tb.KeyPress += new KeyPressEventHandler(timeShopsTextBox_KeyPress);

			e.Control.KeyPress += new KeyPressEventHandler(timeShopsTextBox_KeyPress);
		}


		private void timeShopsTextBox_KeyPress(object sender, KeyPressEventArgs e)
		{
			//when i press enter,bellow code never run?
			if (!(Char.IsDigit(e.KeyChar) || e.KeyChar == 8))
			{
				e.Handled = true;
			}
		}




	}
}
